package com.ssss.danli;

import java.io.*;

/**
 * 为啥建议枚举单例
 * 2.防止序列化问题
 */
public enum SerEnumSingleton implements Serializable {
    INSTANCE;
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 默认就是私有构造
     */
    private SerEnumSingleton() {
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        SerEnumSingleton instance = SerEnumSingleton.INSTANCE;
        instance.setContent("枚举单例序列化");
        System.out.println("枚举序列化前读取其中的内容："+instance.getContent());
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("SerEnumSingleton.obj"));
        oos.writeObject(instance);
        oos.flush();
        oos.close();

        FileInputStream fis = new FileInputStream("SerEnumSingleton.obj");
        ObjectInputStream ois = new ObjectInputStream(fis);
        SerEnumSingleton s1 = (SerEnumSingleton) ois.readObject();
        ois.close();
        System.out.println("vvvv:"+instance+"\n"+s1);
        System.out.println("枚举序列化后读取其中的内容："+s1.getContent());
        System.out.println("枚举序列化前后两个是否同一个:"+(instance == s1));
    }
}
